package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.ApartmentInfo;
import com.atguigu.lease.model.entity.FacilityInfo;
import com.atguigu.lease.model.entity.LabelInfo;
import com.atguigu.lease.web.admin.mapper.ApartmentInfoMapper;
import com.atguigu.lease.web.admin.service.ApartmentInfoService;
import com.atguigu.lease.web.admin.service.GraphInfoService;
import com.atguigu.lease.web.admin.service.RoomInfoService;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
@RequiredArgsConstructor
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo> implements ApartmentInfoService {

    private final GraphInfoService graphInfoService;

    private final RoomInfoService roomInfoService;

    /**
     * 条件分页查询
     */
    @Override
    public IPage<ApartmentItemVo> pageApartmentItemByQuery(Page<ApartmentItemVo> page, ApartmentQueryVo apartmentQueryVo) {
        return baseMapper.pageApartmentItemByQuery(page,apartmentQueryVo);
    }

    /**
     * 根据Id查询公寓详细信息
     */
    @Override
    public ApartmentDetailVo getApartmentDetailVoById(Long id) {

        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();

        // 1. apartmentInfo
        ApartmentInfo apartmentInfo = baseMapper.selectById(id);
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);

        // 2. facility
        List<FacilityInfo> facilityInfos = baseMapper.listFacilityInfoById(id);
        apartmentDetailVo.setFacilityInfoList(facilityInfos);

        // 3. label
        List<LabelInfo> labelInfos = baseMapper.listLabelInfoById(id);
        apartmentDetailVo.setLabelInfoList(labelInfos);

        // 4. feeValue + keyId , keyName
        List<FeeValueVo> feeValueVos = baseMapper.listFeeValueVoById(id);
        apartmentDetailVo.setFeeValueVoList(feeValueVos);

        // 5.GraphVo
        List<GraphVo> graphVos = graphInfoService.list().stream()
                .map(graphInfo -> GraphVo.builder().name(graphInfo.getName()).url(graphInfo.getUrl()).build())
                .collect(Collectors.toList());
        apartmentDetailVo.setGraphVoList(graphVos);

        return apartmentDetailVo;
    }

}




